<?php
    include("dbpw.php");
    include("Node.php");
    class Mysql 
    {
        private static $con;
        public static function clearAllData() 
        {
            mysql_query("DELETE FROM job", Mysql::$con);
        }
        public static function closeConnection() 
        {        
            mysql_close();
        }
        public static function openConnection() 
        {
            Mysql::$con = mysql_connect(Db::$mysql_host, Db::$mysql_user, Db::$mysql_pass);
            if (!Mysql::$con) {
                die('Could not connect: ' . mysql_error());
            }

            mysql_select_db(Db::$mysql_db, Mysql::$con);
        }
        public static function displayData() 
        {
            $result = mysql_query("SELECT * FROM job", Mysql::$con);

            if (!$result) {
                die("Unable to get data: " . mysql_error());
            }
            $i = 1;
            $numclients = mysql_num_rows($result);
            while ($row = mysql_fetch_array($result)) {
                $client = new Client();
                $client->clientip = $row["clientip"];
                $client->clientos = $row["clientos"];
                $client->clienthostname = $row["clienthostname"];
                $client->endframe = $row["endframe"];
                $client->id = $row["idjob"];
                $client->percentdone = $row["percentdone"];
                $client->startframe = $row["startframe"];
                $client->timefinished = $row["timefinished"];
                $client->timeleft = $row["timeleft"];
                $client->timestarted = $row["timestarted"];
                echo $i;
                echo "<br>";
                echo "Hostname: " . $client->clienthostname;
                echo "<br>";
                echo "Ip Address: " . $client->clientip;
                echo "<br>";
                echo "Client OS: " . $client->clientos;
                echo "<br>";
                echo "Start frame: " . $client->startframe;
                echo "<br>";
                echo "End frame: " . $client->endframe;
                echo "<br>";
                echo "Percent done: " . $client->percentdone . "%";
                echo "<br>";
                echo "Time left: " . $client->timeleft;
                echo "<br>";
                echo "Start date: " . $client->timestarted;
                echo "<br>";
                if ($client->percentdone == 100 || $row["abortrequested"]) {
                    echo "Finished date: " . $client->timefinished; 
                    echo "<br>";
                }
                if ($row["abortrequested"]) {
                    echo "<b>Aborted!</b>";
                    echo "<br>";
                }
                echo "<a href='abort.php?id=" . $client->id . "'>Abort</a>";
                echo "<br>";
                echo "<br>";

                $clients[$i] = $client;
                
                $i++;
            }
            if (!empty ($clients))
                return $clients;
        }
        public static function getConnection() 
        {
            return Mysql::$con;
        }
    }
?>
